package com.ws.ucenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ws.commonutils.JwtUtils;
import com.ws.commonutils.MD5;
import com.ws.servicebase.exceptionhandler.MyException;
import com.ws.ucenterservice.entity.UcenterMember;
import com.ws.ucenterservice.entity.vo.LoginUser;
import com.ws.ucenterservice.entity.vo.RegisterUser;
import com.ws.ucenterservice.mapper.UcenterMemberMapper;
import com.ws.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author ws
 * @since 2024-12-11
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(LoginUser loginUser) {
        String mobile = loginUser.getMobile();
        String password = loginUser.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new MyException(20001,"登录失败");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember member = baseMapper.selectOne(wrapper);
        if (member == null) {
            throw new MyException(20001,"登录失败");
        }
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new MyException(20001,"登录失败");
        }

        if (member.getIsDisabled()){
            throw new MyException(20001,"登录失败");
        }
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());

        return token;
    }

    @Override
    public Boolean register(RegisterUser registerUser) {
        String nickname = registerUser.getNickname();
        String password = registerUser.getPassword();
        String code = registerUser.getCode();
        String mobile = registerUser.getMobile();
        if (StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)) {
            throw new MyException(20001,"注册失败");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer i = baseMapper.selectCount(wrapper);
        if (i > 0) {
            throw new MyException(20001,"注册失败");
        }

        if(!code.equals(redisTemplate.opsForValue().get(mobile))){
            throw new MyException(20001,"注册失败");
        }

        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setMobile(mobile);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        int insert = baseMapper.insert(member);
        return insert>0;

    }

    @Override
    public LoginUser getLoginUser(String id) {
        UcenterMember member = baseMapper.selectById(id);
        LoginUser loginUser = new LoginUser();
        BeanUtils.copyProperties(member, loginUser);
        return loginUser;
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer getCountRegister(String day) {
        Integer cnt=baseMapper.getCountRegister(day);
        return cnt;
    }
}
